package com.example.rotateimageview.draghelp

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.customview.widget.ViewDragHelper
import com.example.rotateimageview.R

/**
 * author  : Liushuai
 * time    : 2021/10/16 20:37
 * desc    :滑动侧边菜单布局 带缩放动画
 */
class SlideMenuGroup @JvmOverloads constructor(
        context: Context,
        attributeSet: AttributeSet?=null
) : FrameLayout(context,attributeSet) {

    private lateinit var mMainView:View
    private lateinit var  mDragger : ViewDragHelper

    private var mMenuViewWidth=200
    private lateinit var mMenuView:View


    init {
        initView()
    }



    private fun initView() {
        mDragger=ViewDragHelper.create(this, object : ViewDragHelper.Callback() {
            override fun tryCaptureView(child: View, pointerId: Int): Boolean {
                return child == mMainView
            }

            override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int {
                if (left > 0) {
                    return Math.min(left, mMenuViewWidth)
                }
                return 0
            }

            override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) {
                super.onViewReleased(releasedChild, xvel, yvel)
                if (mMainView.left < mMenuViewWidth / 2) {
                    mDragger.smoothSlideViewTo(mMainView,0,0)
                }else{
                    mDragger.smoothSlideViewTo(mMainView,mMenuViewWidth,0)
                }
                invalidate()
            }

            override fun onViewPositionChanged(
                changedView: View,
                left: Int,
                top: Int,
                dx: Int,
                dy: Int
            ) {
                super.onViewPositionChanged(changedView, left, top, dx, dy)
                val percent = mMainView.left / mMenuViewWidth.toFloat()
                mMenuView.scaleX = 0.5f + 0.5f * percent
                mMenuView.scaleY = 0.5f + 0.5f * percent
                mMenuView.translationX=-mMenuViewWidth /2 + mMenuViewWidth /2 * percent
                //缩放 1- 0.8
                mMainView.scaleY=1 - percent * 0.2f
                mMainView.scaleX=1 - percent * 0.2f
            }
        })
    }

    override fun computeScroll() {
        if (mDragger.continueSettling(true)) {
            invalidate()
        }
    }

    fun setMainView(mainView: View,layoutParams:LayoutParams) {
        this.mMainView=mainView
        addView(mainView,layoutParams)
    }

    fun setMenuView(menuView:View,layoutParams:LayoutParams){
        this.mMenuView=menuView
        mMenuViewWidth=layoutParams.width
        addView(menuView,layoutParams)
    }

   fun closeMenuView() {
        mDragger.smoothSlideViewTo(mMainView,0,0)
       ViewCompat.postInvalidateOnAnimation(this)
    }

    fun setMainViewContent(content:String) {
        mMainView.findViewById<TextView>(R.id.tv_mainView).setText(content)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return mDragger.shouldInterceptTouchEvent(ev!!)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        mDragger.processTouchEvent(event!!)
        return true
    }
}